//
//  SelectMonthAndDayCell.h
//  CommunityNannyO2O
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "SelectMonthAndDayCell.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "CustomTextField.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "SelectExamTimeSignUpCell.h"
#import "NIDropDown.h"
#import "YearSubCell.h"

@interface SelectMonthAndDayCell()<QXNetWorkToolDelegate,
                                    UITableViewDataSource,UITableViewDelegate,
                                    UIPickerViewDataSource, UIPickerViewDelegate,
                                    NIDropDownDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *monthPickerView;
@property (weak, nonatomic) IBOutlet UIPickerView *dayPickerView;

@property (weak, nonatomic) IBOutlet UILabel *yearLabel;

@property (strong, nonatomic) NSMutableArray *dayData;
@property (strong, nonatomic) NSMutableArray *monthData;
@property (nonatomic, strong) NSMutableArray *dayAryOfCurMonth;

@property (nonatomic, copy) NSString *selectMonth;
@property (nonatomic, copy) NSString *selectDay;

@property (copy, nonatomic) NSString *selectStartHour;
@property (copy, nonatomic) NSString *selectStartMinute;
@property (copy, nonatomic) NSString *selectEndHour;
@property (copy, nonatomic) NSString *selectEndMinute;
@property (weak, nonatomic) IBOutlet UIButton *confirm;
@property (strong, nonatomic) NIDropDown *dropDown;
@property (assign, nonatomic) BOOL isDropDown;
@property (strong, nonatomic) NSMutableArray *yearList;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *yearViewConstraint;

@property (nonatomic,assign) NSInteger yearSelectIndex;

@property (nonatomic,strong) NSMutableDictionary *dataDict;

@end

@implementation SelectMonthAndDayCell

@synthesize dayData;
@synthesize monthData;
@synthesize dayAryOfCurMonth;
@synthesize yearList;

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"SelectMonthAndDayCell";
    SelectMonthAndDayCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil]firstObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}

-(void)awakeFromNib
{
    self.dayPickerView.delegate = self;
    self.dayPickerView.dataSource = self;
    self.dayPickerView.backgroundColor = [UIColor clearColor];
    
    self.monthPickerView.delegate = self;
    self.monthPickerView.dataSource = self;
    self.monthPickerView.backgroundColor = [UIColor clearColor];
    
    self.monthData = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4",@"5",
                      @"6", @"7", @"8", @"9",@"10", @"11",@"12",nil];
    
    [self initMonthAndDay];
    
    self.confirm.layer.cornerRadius = 5;
    self.confirm.layer.masksToBounds = YES;
    
    //TODO:wanglei
    self.yearList = [NSMutableArray arrayWithObjects:@"2015", @"2016", @"2017", nil];
    
    self.yearSelectIndex = 0;
    
    for (int i = 0; i < [self.yearList count]; ++i)
    {
        NSString *str = [self.yearList objectAtIndex:i];
        str = [str stringByAppendingString:@"年"];
        [self.yearList replaceObjectAtIndex:i withObject:str];
    }
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelMenu)];

    [self addGestureRecognizer:tapGestureRecognizer];
    tapGestureRecognizer.delegate = self;
    
    [self configDate];
}

-(void)initMonthAndDay
{
    NSMutableArray *ary28 = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 28; ++i)
    {
        [ary28 addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    NSMutableArray *ary29 = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 29; ++i)
    {
        [ary29 addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    NSMutableArray *ary30 = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 30; ++i)
    {
        [ary30 addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    NSMutableArray *ary31 = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 31; ++i)
    {
        [ary31 addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    //天
    [self.dayData removeAllObjects];
    self.dayData = [[NSMutableArray alloc] init];
    
    [self.dayData addObject:ary31];//1
    
    //2月
    int year = [self.yearLabel.text intValue];
    if (year % 4 == 0)//闰年
    {
        [self.dayData addObject:ary29];
    }
    else //平年
    {
        [self.dayData addObject:ary28];
    }
    
    [self.dayData addObject:ary31];//3
    [self.dayData addObject:ary30];//4
    [self.dayData addObject:ary31];//5
    [self.dayData addObject:ary30];//6
    [self.dayData addObject:ary31];//7
    [self.dayData addObject:ary31];//8
    [self.dayData addObject:ary30];//9
    [self.dayData addObject:ary31];//10
    [self.dayData addObject:ary30];//11
    [self.dayData addObject:ary31];//12
    
    [self.dayAryOfCurMonth removeAllObjects];
    self.dayAryOfCurMonth = [[NSMutableArray alloc] init];
    self.dayAryOfCurMonth = [self.dayData objectAtIndex:0];
    
    self.selectMonth = @"1";
    self.selectDay = @"1";
    
    [self pickerView:self.monthPickerView didSelectRow:0 inComponent:0];
    [self pickerView:self.dayPickerView didSelectRow:0 inComponent:0];
}

-(void)initMenu
{
    UIView *viewYear = [self viewWithTag:777];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showYear)];
    
    [viewYear addGestureRecognizer:tapGestureRecognizer];
    
    //下拉菜单
    CGFloat tmpX = self.yearViewConstraint.constant + viewYear.frame.size.width / 2;
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
            tmpX -= 0;
        tmpX -= 3;
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        tmpX = ([UIScreen mainScreen].applicationFrame.size.width - viewYear.frame.size.width) / 2;
        
        tmpX -= 11.5;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        tmpX = ([UIScreen mainScreen].applicationFrame.size.width - viewYear.frame.size.width) / 2;
        
        tmpX -= 11.8;
    }
    
    self.dropDown = [[NIDropDown alloc]initDropDown:viewYear
                                                  X:tmpX
                                                  Y:viewYear.frame.origin.y
                                             Height:viewYear.frame.size.height * self.yearList.count - 1];
    
    self.dropDown.delegate = self;
    self.isDropDown = YES;
    self.dropDown.cellBankGroundColor = UIColorRGBA(237, 237, 237, 1);
}

//下拉菜单代理
- (NSInteger)NIDropDownNumberOfSectionsInTableView:(UITableView *)tableView Column:(NSInteger)column
{
    return 1;
}

-(NSInteger)NIDropDownTableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section Column:(NSInteger)column
{
    return [self.yearList count];
}

- (CGFloat)NIDropDownTableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath Column:(NSInteger)column
{
    return 26;
}

- (UITableViewCell *)NIDropDownTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath Column:(NSInteger)column
{
    YearSubCell *cell = [YearSubCell cellWithTableView:tableView];
    
    if ([self.yearList count])
    {
        cell.yearLabel.text = [self.yearList objectAtIndex:indexPath.row];
        
        if (self.yearSelectIndex == indexPath.row)
        {
            cell.check.hidden = NO;
        }
    }
    
    return cell;
}

- (void)NIDropDownTableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath Column:(NSInteger)column
{
    NSArray *array = [tableView visibleCells];
    
    for (YearSubCell *cell in array)
    {
        cell.check.hidden = YES;
    }
    
    YearSubCell *cell = (YearSubCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (cell)
    {
        cell.check.hidden = NO;
        [self selectTitle:cell.yearLabel.text];
        
        self.yearSelectIndex = indexPath.row;
    }
    
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    if (cell)
//    {
//        [self selectTitle:cell.textLabel.text];
//        
//        
////        [cell.textLabel setFrame:CGRectMake(cell.textLabel.frame.origin.x + 20,
////                                           cell.textLabel.frame.origin.y,
////                                           cell.textLabel.frame.size.width,
////                                            cell.textLabel.frame.size.height)];
//        
//        UIImage *image= [UIImage imageNamed:@"bt_check"];
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        
//        CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
//        
//        button.frame = frame;
//        
//        [button setBackgroundImage:image forState:UIControlStateNormal];
//        
//        button.backgroundColor = [UIColor clearColor];
//        
//        cell.accessoryView = button;
//    }
}

-(void)showYear
{
    if (self.isDropDown)
        [self.dropDown showDropDownOfColumn:0];
    else
        [self.dropDown hideDropDown];
    
    self.isDropDown = !self.isDropDown;
}

- (void)selectTitle:(NSString*)title
{
    self.yearLabel.text = title;
    self.isDropDown = !self.isDropDown;
    
    [self initMonthAndDay];
    
    if ([self.delegate respondsToSelector:@selector(selectYear:)])
    {
        [self.delegate selectYear:title];
    }
}

-(void)cancelMenu
{
    [self.dropDown hideDropDown];
    self.isDropDown = NO;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"])
    {
        //如果当前是tableView
        //做自己想做的事
        return NO;
    }
    return YES;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView.tag == 0)    //月
    {
        return [self.monthData count];
    }
    else    //日
    {
        return [self.dayAryOfCurMonth count];
    }
    
    return 0;
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView.tag == 0)    //月
    {
        return [self.monthData objectAtIndex:row];
    }
    else    //日
    {
        return [self.dayAryOfCurMonth objectAtIndex:row];
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == 0)    //月
    {
        self.dayAryOfCurMonth = [self.dayData objectAtIndex:row];
        
        [self.dayPickerView selectRow:0 inComponent:0 animated:YES];
        
        //        重新装载第二个滚轮中的值
        [self.dayPickerView reloadComponent:0];
        
        //设置月份
        self.selectMonth = [self.monthData objectAtIndex:row];
        
        //默认第一天
        self.selectDay = [self.dayAryOfCurMonth objectAtIndex:0];
    }
    else
    {
        self.selectDay = [self.dayAryOfCurMonth objectAtIndex:row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 37.0;
}

-(IBAction)finish:(id)sender
{
    if ([self.delegate respondsToSelector:
            @selector(selectMonth:Day:)])
    {
        [self.delegate selectMonth:self.selectMonth Day:self.selectDay];
    }
}



-(void)configDate{

    NSString * data = @"2015-11-30";

    NSString * data1 = [self configDate:data dayNum:1];
    
    NSString * data2 = [self configDate:data dayNum:2];

    NSString * data3 = [self configDate:data dayNum:3];

    NSString * day1 = [data1 substringFromIndex:6];
    
    NSString * day2 = [data2 substringFromIndex:6];
    
    NSString * day3 = [data3 substringFromIndex:6];
    
    NSString * month1= [data1 substringWithRange:NSMakeRange(4,2)];
    
    NSString * month2= [data2 substringWithRange:NSMakeRange(4,2)];
    
    NSString * month3= [data3 substringWithRange:NSMakeRange(4,2)];

    NSString * year1 = [data1 substringToIndex:4];
    
    NSString * year2 = [data2 substringToIndex:4];
    
    NSString * year3 = [data3 substringToIndex:4];
    
    NSMutableArray * moutharray = [NSMutableArray array];
    
    NSMutableDictionary * mouthDict1 = [NSMutableDictionary dictionary];
    
    NSMutableDictionary * mouthDict2 = [NSMutableDictionary dictionary];
    
  
    
    _dataDict = [NSMutableDictionary dictionary];

    
    if (![year1 isEqualToString:year2]) {
        
    
        [mouthDict1 setObject:@[day1] forKey:month1];
        
        [mouthDict2 setObject:@[day2,day3] forKey:month1];
        
        
        [moutharray addObject:mouthDict1];

         [moutharray addObject:mouthDict2];
        
        
        [_dataDict setObject:moutharray forKey:year1];


        
    }else if (![year2 isEqualToString:year3]){
    
    
    
    }else {
    
        if (! [month1 isEqualToString:month2]) {
            
            [mouthDict1 setObject:@[day1] forKey:month1];
            
            [mouthDict2 setObject:@[day2,day3] forKey:month1];
            
            [moutharray addObject:mouthDict1];
            
            [moutharray addObject:mouthDict2];
            
        }else if (![month2 isEqualToString:month3]){
            
            [mouthDict1 setObject:@[day1,day2] forKey:month1];
            
            [mouthDict2 setObject:@[day3] forKey:month2];
            
            [moutharray addObject:mouthDict1];
            
            [moutharray addObject:mouthDict2];
            
        }else {
            
            [mouthDict1 setObject:@[day1,day2,day3] forKey:month1];
            
            [moutharray addObject:mouthDict1];
            
        }
    
        
        [_dataDict setObject:moutharray forKey:year1];
    }
    
    
    
    
    [_dataDict setObject:@"1" forKey:year1];
    
    [_dataDict setObject:@"2" forKey:year2];
    
    [_dataDict setObject:@"3" forKey:year3];
   
    
    
}


- (NSString *)configDate:(NSString *)dateString dayNum:(int)num{
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc]init];
    
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate* inputDate = [inputFormatter dateFromString:dateString];
    
    NSTimeInterval  interval =24*60*60*num;

    
    [inputFormatter setDateFormat:@"yyyyMMdd"];
    
    NSString *titleString = [inputFormatter stringFromDate:[inputDate initWithTimeInterval:interval sinceDate:inputDate]];
    
    return titleString;
}









@end




















